%% Author: kevin
%% Created: 2009-05-29
%% Description: Calculate the sum of all prime numbers below 2M
-module(p10).

%%
%% Include files
%%

%%
%% Exported Functions
%%
-export([solve/0]).

%%
%% API Functions
%%
solve() ->
    sum_all_primes_under(2000000).

sum_all_primes_under(To) ->
    sieve(lists:seq(2, To), To).

sieve([], _) -> 0;
sieve(List, Range) ->
    [Max|_] = lists:reverse(List),
    case Max < math:sqrt(Range) of
        true -> List;
        false ->
            [H|Rest] = List,
            io:format("~p ~n", [H]),
            H + sieve(lists:filter(fun(X)->X rem H =/= 0 end, Rest), Range)
    end.

